﻿// 2. Write a program that reads two arrays from the console and compares them element by element.

using System;

class Compare2matrix
{
    static void Main()
    {
        // get the length of first array
        Console.Write("Enter length of the first array : ");
        string size1 = Console.ReadLine();
        int sizeOfArray1;
        while (!int.TryParse(size1, out sizeOfArray1))
        {
            Console.Write("Invalid input. Enter again length for the first array : ");
            size1 = Console.ReadLine();
        }

        // get the length of second array
        Console.Write("Enter length of the second array : ");
        string size2 = Console.ReadLine();
        int sizeOfArray2;
        while (!int.TryParse(size2, out sizeOfArray2))
        {
            Console.Write("Invalid input. Enter again length for the second array : ");
            size2 = Console.ReadLine();
        }

        if (sizeOfArray1 <= 0 || sizeOfArray2 <= 0)
        {
            // if the values for length are negative or 0 they cannot be lengths for an array
            Console.WriteLine("The length of an array should be positive number. Start your program again.");
        }
        else
        {
            if (sizeOfArray1 != sizeOfArray2)
            {
                // if the lengths are different then the arryas cannot be compared element by element
                Console.WriteLine("These arrays cannot be compared because they have different lengths.");
            }
            else
            {
                // if the lengths are the same then start to enter in console first the element of the first array
                // and then the element of the second array; 
                bool equalArrays = true;
                int[] array1 = new int[sizeOfArray1];
                int[] array2 = new int[sizeOfArray2];

                for (int index = 0; index < sizeOfArray1; index++)
                {
                    // get the element of the first array
                    Console.Write("Enter element of the first array : ");
                    string element1 = Console.ReadLine();
                    int elementOfArray1;
                    while (!int.TryParse(element1, out elementOfArray1))
                    {
                        Console.Write("Invalid input. Enter element again : ");
                        element1 = Console.ReadLine();
                    }
                    array1[index] = elementOfArray1;

                    // get the element of the second array
                    Console.Write("Enter element of the second array : ");
                    string element2 = Console.ReadLine();
                    int elementOfArray2;
                    while (!int.TryParse(element2, out elementOfArray2))
                    {
                        Console.Write("Invalid input. Enter element again : ");
                        element2 = Console.ReadLine();
                    }
                    array2[index] = elementOfArray2;

                    if (elementOfArray1 != elementOfArray2)
                    {
                        // if the entered elements are different then the boolean variable gets value false
                        equalArrays = false;
                    }
                }

                if (equalArrays)
                {
                    // if the boolean variable has value TRUE then the arrays are equal
                    Console.WriteLine();
                    Console.WriteLine("The elements of both arrays are equal.");
                }
                else
                {
                    // if the boolean variable has value FALSE then the arrays are NOT equal
                    Console.WriteLine();
                    Console.WriteLine("The elements of both arrays are NOT equal.");
                }

                // prints the values of the arrays
                Console.WriteLine();
                string stringArray1 = "Array 1";
                string stringArray2 = "Array 2";
                Console.WriteLine("{0, 21} {1,10}", stringArray1, stringArray2);

                for (int index = 0; index < sizeOfArray1; index++)
                {
                    Console.Write("array [{0}] : ", index);
                    Console.Write("{0, 10} ", array1[index]);
                    Console.WriteLine("{0, 10} ", array2[index]);
                }
            }
        }

    }
}
